import Vue from 'vue'
import Router from 'vue-router'
import BaseLoading from 'views/base/loading'
import BaseLogin from 'views/base/login'
import BaseRegister from 'views/base/register'
import Sorts from 'views/sorts/index'
import SortsDetail from 'views/sorts/detail'
import SortsSearch from 'views/sorts/search'
import ActivityDetail from 'views/activity/detail'
import Home from 'views/home/index'
import HomeCity from 'views/home/city'
import Test from 'views/home/test'
import Cart from 'views/cart/index'
import User from 'views/user/index' // 用户页
import UserSetting from 'views/user/setting' // 用户设置
import UserAddress from 'views/user/address' // 用户地址
import UserContact from 'views/user/contact' // 联系客服
import UserCoupon from 'views/user/coupon' // 优惠券
import UserPolicy from 'views/user/policy' // 服务政策
import UserIntegral from 'views/user/integral' // 积分兑换
import UserOften from 'views/user/often' // 常购买
import OrderLists from 'views/order/lists' // 订单
import QuickAdd from 'views/quick/add' // 添加快速订单
import QuickLists from 'views/quick/lists' // 自定义订单
import store from 'src/store'
let _self = window

Vue.use(Router)

// 滚动条控制
const scrollBehavior = to => {
  const position = {}
  if (to.hash) {
    position.selector = to.hash
  }
  if (to.matched.some(mm => mm.meta.scrollToTop)) {
    position.x = 0
    position.y = 0
  }
  return position
}
// 安全验证
const fairGuardRoute = (to, from, next) => {
  let token = _self.$cache.get('session_token')
  if (token && store.getters.getProfile) {
    next()
  } else {
    next({name: 'login', params: {uri: encodeURIComponent(window.location.href)}})
  }
}
// router 定义
let routes = [
  {
    name: 'login',
    meta: {title: '用户登录'},
    path: '/login/:uri',
    component: BaseLogin
  },
  {
    name: 'register',
    meta: {title: '用户注册'},
    path: '/register',
    component: BaseRegister
  },
  {
    name: 'loading',
    meta: {title: '加载中'},
    path: '/',
    component: BaseLoading
  },
  {
    name: 'test',
    meta: {title: '测试'},
    path: '/test',
    component: Test
  },
  {
    name: 'home:index',
    meta: {title: '首页'},
    path: '/home/index',
    component: Home
  },
  {
    name: 'home:city',
    meta: {title: '选择城市'},
    path: '/home/city',
    component: HomeCity
  },
  {
    name: 'cart:index',
    meta: {title: '购物车'},
    path: '/cart/index',
    component: Cart
  },
  {
    name: 'sorts:index',
    meta: {title: '分类'},
    path: '/sorts/index/:id',
    component: Sorts
  },
  {
    name: 'quick:add',
    meta: {title: '快速下单'},
    path: '/quick/add',
    component: QuickAdd
  },
  {
    name: 'quick:lists',
    meta: {title: '自定义定单'},
    path: '/quick/lists/:type',
    component: QuickLists
  },
  {
    name: 'sorts:search',
    meta: {title: '查询'},
    path: '/sorts/search/:key',
    component: SortsSearch
  },
  {
    name: 'sorts:detail',
    meta: {title: '产品详细'},
    path: '/sorts/detail/:id/:aId',
    component: SortsDetail
  },
  {
    name: 'activity:detail',
    meta: {title: '活动详细'},
    path: '/activity/detail/:id',
    component: ActivityDetail
  },
  {
    name: 'user:index',
    meta: {title: '我的'},
    beforeEnter: fairGuardRoute,
    path: '/user/index',
    component: User
  },
  {
    name: 'user:setting',
    meta: {title: '设置'},
    beforeEnter: fairGuardRoute,
    path: '/user/setting',
    component: UserSetting
  },
  {
    name: 'user:integral',
    meta: {title: '积分兑换'},
    beforeEnter: fairGuardRoute,
    path: '/user/integral',
    component: UserIntegral
  },
  {
    name: 'order:lists',
    meta: {title: '我的订单'},
    beforeEnter: fairGuardRoute,
    path: '/order/lists/:type',
    component: OrderLists
  },
  {
    name: 'user:often',
    meta: {title: '我的收藏'},
    beforeEnter: fairGuardRoute,
    path: '/user/often',
    component: UserOften
  },
  {
    name: 'user:policy',
    meta: {title: '服务政策'},
    beforeEnter: fairGuardRoute,
    path: '/user/policy',
    component: UserPolicy
  },
  {
    name: 'user:coupon',
    meta: {title: '优惠券'},
    beforeEnter: fairGuardRoute,
    path: '/user/coupon',
    component: UserCoupon
  },
  {
    name: 'user:contact',
    meta: {title: '联系客服'},
    beforeEnter: fairGuardRoute,
    path: '/user/contact',
    component: UserContact
  },
  {
    name: 'user:address',
    meta: {title: '地址管理'},
    beforeEnter: fairGuardRoute,
    path: '/user/address',
    component: UserAddress
  }
]
let router = new Router({
  mode: 'hash',
  base: './',
  scrollBehavior,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})
export default router
